package cn.tianjindong.blog_manager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tianjindong.blog.common.pojo.PageView;
import cn.tianjindong.blog.mapper.BlogMessageBoardMapper;
import cn.tianjindong.blog.pojo.BlogArticleComment;
import cn.tianjindong.blog.pojo.BlogArticleCommentExample;
import cn.tianjindong.blog.pojo.BlogMessageBoard;
import cn.tianjindong.blog.pojo.BlogMessageBoardExample;
import cn.tianjindong.blog_manager.service.MessageService;

@Service
public class MessageServiceImpl implements MessageService {

	@Autowired
	private BlogMessageBoardMapper messageMapper;

	@Override
	public PageView<BlogMessageBoard> getMessagePageView(Integer currentPage, Integer countItem) {
		// 查询时开始索引
		int startIndex = (currentPage - 1) * countItem;
		List<BlogMessageBoard> list = messageMapper.getMessageList(startIndex, countItem);

		// 获取数据库总条数
		int totalItem = messageMapper.countByExample(new BlogMessageBoardExample());

		PageView<BlogMessageBoard> pageView = new PageView<>();
		pageView.setCurrentPage(currentPage);
		pageView.setCountItem(countItem);
		pageView.setItems(list);
		pageView.setTotalItem(totalItem);
		return pageView;
	}

	@Override
	public BlogMessageBoard getMessageById(Long messageId) {
		BlogMessageBoard message = messageMapper.getMessageById(messageId);
		if (message != null) {
			BlogMessageBoard tempMessage = new BlogMessageBoard();
			tempMessage.setId(messageId);
			tempMessage.setTab(1);// 标记位已读
			messageMapper.updateByPrimaryKeySelective(tempMessage);
		}
		return message;
	}

	@Override
	public void deleteById(Long messageId) {
		messageMapper.deleteByPrimaryKey(messageId);
	}

	@Override
	public void reverseTagReply(Long messageId) {
		BlogMessageBoard message = messageMapper.selectByPrimaryKey(messageId);
		if (message != null) {
			BlogMessageBoard tempMessage = new BlogMessageBoard();
			tempMessage.setId(messageId);
			if(message.getTagReply()==0){
				tempMessage.setTagReply(1);
			}else{
				tempMessage.setTagReply(0);
			}
			messageMapper.updateByPrimaryKeySelective(tempMessage);
		}
	}

}
